<template>
  <div class="page-container">
    <div class="page-content">
      <!--搜索-->
      <div class="panel-container">
        <searchuinew ref="searchuinew" :searchFormArr="searchFormArr" @searchParma="searchParma" :isAccording="isAccording"></searchuinew>
      </div>
      <div class="panel-container no-margin">
        <p class="line">业绩（元）: {{ totalFee }}</p>
        <el-table :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" v-loading="dataListLoading">
          <el-table-column align="center" header-align="center" label="序号" type="index" width="100"></el-table-column>
          <el-table-column align="center" header-align="center" label="公司名称" prop="company_name"></el-table-column>
          <el-table-column align="center" header-align="center" label="联系人" prop="contact_person"></el-table-column>
          <el-table-column align="center" header-align="center" label="联系电话" prop="mobile_phone"></el-table-column>
          <el-table-column align="center" header-align="center" label="会员动态" prop="membership_level_change"></el-table-column>
          <el-table-column align="center" header-align="center" label="上一级会员等级" prop="front_member_level"> </el-table-column>
          <el-table-column align="center" header-align="center" label="当前会员等级" prop="membership_level"></el-table-column>
          <el-table-column align="center" header-align="center" label="在回收商年份（年）" prop="years_in_recycling"></el-table-column>
          <el-table-column align="center" header-align="center" label="业绩（元）" prop="service_fee_amount_num"></el-table-column>
          <el-table-column align="center" header-align="center" label="渠道">
            <template> hss </template>
          </el-table-column>
          <el-table-column align="center" header-align="center" label="更新时间" prop="created_time"></el-table-column>
          <el-table-column align="center" fixed="right" header-align="center" label="操作" width="100">
            <template slot-scope="scope">
              <el-button size="mini" type="text" class="grey" @click="handleShop(scope)">店铺</el-button>
              <el-button size="mini" type="text" class="grey" @click="handleDetail(scope)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageIndex"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="totalPage"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { apiMemberUpdate } from '@/api/industryData'
import { formatFormData } from '@/utils'
export default {
  name: 'index',
  data() {
    const vipLevelList = [
      { id: 1, text: '钻石VIP' },
      { id: 2, text: '银钻VIP' },
      { id: 3, text: '金钻VIP' },
      { id: 4, text: '财富通至尊' },
    ]
    return {
      vipLevelList: vipLevelList,
      searchFormArr: [
        {
          fieldName: '企业名称：',
          type: 'input',
          field: 'company_name',
          placeholder: '请输入企业名称',
        },
        {
          fieldName: '联系电话：',
          type: 'input',
          field: 'phone',
          placeholder: '请输入联系电话',
        },
        {
          fieldName: '会员动态：',
          type: 'select',
          field: 'membership_level_change',
          option: [
            { id: '新增', text: '新增' },
            { id: '升级', text: '升级' },
            { id: '降级', text: '降级' },
            { id: '续费', text: '续费' },
            { id: '过期', text: '过期' },
          ],
          placeholder: '请选择会员动态',
        },
        {
          fieldName: '渠道：',
          type: 'select',
          field: 'source',
          option: [{ id: '回收商', text: 'hss' }],
          placeholder: '请选择渠道',
        },
        {
          fieldName: '当前会员等级：',
          type: 'select',
          field: 'membership_level_code',
          option: vipLevelList,
          placeholder: '请选择会员等级',
        },
        {
          fieldName: '更新时间',
          type: 'timePicker',
          field: 'time',
          placeholder: '请选择更新时间',
          timeType: 'daterange',
          format: 'yyyy-MM-dd',
        },
      ],
      isAccording: true,
      searchParams: {
        company_name: '',
        phone: '',
        membership_level_change: '',
        source: '',
        membership_level_code: '',
        create_start_time: '',
        create_end_time: '',
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      totalFee: 0,
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      if (this.$route.query.source) {
        const { change, start_time, end_time, source } = this.$route.query
        if (change) {
          this.$refs.searchuinew.clearFormItem('membership_level_change', change)
        }
        if (start_time) {
          this.$refs.searchuinew.clearFormItem('time', [start_time, end_time])
        }
        if (source) {
          this.$refs.searchuinew.clearFormItem('source', '回收商')
        }
      } else {
        this.getDataList()
      }
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const formData = formatFormData(Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize }, this.searchParams))

      apiMemberUpdate(formData)
        .then((res) => {
          const { items, total } = res.result
          this.dataList = items
          this.totalPage = total
          this.totalFee = res.total_fee
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.totalPage = 0
          this.dataListLoading = false
        })
    },

    // 列表查询
    searchParma(e) {
      this.searchFormArr.forEach((item) => {
        if (item.type === 'timePicker') {
          return
        }
        if (e[item.field] || e[item.field] === 0) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })
      if (e.time) {
        this.searchParams.create_start_time = e.time[0]
        this.searchParams.create_end_time = e.time[1]
      } else {
        this.searchParams.create_start_time = this.searchParams.create_end_time = ''
      }
      this.pageIndex = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 店铺
    handleShop(scrop) {
      window.open(scrop.row.store_link)
    },
    // 详情
    handleDetail(scope) {
      this.$toast('暂未开通')
    },
    prevVIPLevel(code) {
      const item = this.vipLevelList.find((item) => item.id === parseInt(code - 1))
      if (item) {
        return item.text
      } else {
        return '-'
      }
    },
  },
}
</script>
